﻿// $Id$
/*! \file
 * \brief Definition of Northwind.DomainModel.Data.OrderDetailsMapper.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// <summary />
    ///
    ///////////////////////////////////////////////////////////////////////////

    public partial class OrderDetailsMapper:
        DataMapper<OrderDetails, OrderDetails.PrimaryKey, OrderDetails.DataTransfer>,
        IOrderDetailsMapper
    {

        /// <summary />
        public OrderDetailsMapper(DbConnection connection, ICacheManager cacheManager):
            base(connection, DomainModelCreator.CreateDefault<OrderDetails>(), cacheManager)
        {
            _ParameterBuilder=new ParameterBuilder(connection);
        }

        /// <summary />
        public DomainModelKeyedCollection<OrderDetails> FindAll(DataMapperCollection dataMappers)
        {
          return _FindAll(dataMappers);
        }

        /// <summary />
        public DomainModelKeyedCollection<OrderDetails> FindByOrder(Order order, DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.OrderDetailsSelectByOrder,
                ParameterBuilder.GetMarker("OrderID", 1)
            );

            IDbDataParameter p=commands[0].CreateParameter();
            p.ParameterName=ParameterBuilder.GetName("OrderID", 1);
            p.DbType=DbType.Int32;
            p.Value=order.GetId().Value;
            p.Direction=ParameterDirection.Input;
            commands[0].Parameters.Add(p);

            return Load(commands, dataMappers);
        }

        /// <summary />
        protected override IList<IDbCommand> CreateSelectCommands(OrderDetails.PrimaryKey key)
        {
            return CreateSelectCommands(Connection, ParameterBuilder, key);
        }

        /// <summary />
        protected override IList<IDbCommand> CreateDeleteCommands(OrderDetails.DataTransfer data)
        {
            return CreateDeleteCommands(Connection, ParameterBuilder, (OrderDetails.DataTransfer)data);
        }

        /// <summary />
        protected override IList<IDbCommand> CreateInsertCommands(OrderDetails.DataTransfer data)
        {
            return CreateInsertCommands(Connection, ParameterBuilder, (OrderDetails.DataTransfer)data);
        }

        /// <summary />
        protected override IList<IDbCommand> CreateUpdateCommands(OrderDetails.DataTransfer data)
        {
            return CreateUpdateCommands(Connection, ParameterBuilder, (OrderDetails.DataTransfer)data);
        }

        /// <summary />
        protected override OrderDetails.DataTransfer GetDataTransferObject(IList<IDataRecord> records)
        {
          return _GetDataTransferObject(records);
        }

        /// <summary />
        internal static IList<IDbCommand> CreateSelectCommands(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.PrimaryKey key)
        {
            return new IDbCommand[] { _CreateSelectCommand(connection, parameterBuilder, key) };
        }

        /// <summary />
        internal static IList<IDbCommand> CreateDeleteCommands(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            return new IDbCommand[] { _CreateDeleteCommand(connection, parameterBuilder, (OrderDetails.DataTransfer)data) };
        }

        /// <summary />
        internal static IList<IDbCommand> CreateInsertCommands(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            return new IDbCommand[] { _CreateInsertCommand(connection, parameterBuilder, (OrderDetails.DataTransfer)data) };
        }

        /// <summary />
        internal static IList<IDbCommand> CreateUpdateCommands(IDbConnection connection, ParameterBuilder parameterBuilder, OrderDetails.DataTransfer data)
        {
            return new IDbCommand[] { _CreateUpdateCommand(connection, parameterBuilder, (OrderDetails.DataTransfer)data) };
        }

    }
}
